
package com.cqvie.controller;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import javax.annotation.Resource;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqvie.model.User;
import com.cqvie.service.StudentService;
import com.cqvie.service.TeacherService;
//import com.cqvie.service.TeacherService;
import com.cqvie.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController_old{
	@Resource
	public StudentService studentService;
	@Resource
	public TeacherService teacherService;
	@Resource
	public UserService userService;
	
/*
	@ResponseBody
	@RequestMapping(value="/adminlogin")
	public String login( User user){
		boolean loginResult=userService.isExist(user);
		System.out.println("登录结果："+loginResult);
		return "login_result";
	}
	
	@ResponseBody
	@RequestMapping(value="/test")
	public String test( User user){
		boolean loginResult = userService.isExist(user);
		return loginResult+"";
	}
	
	
	@RequestMapping(value="/testJson")
	public @ResponseBody User testJson(@RequestBody User user){
		System.out.println(user);
		return user;
	}
*/
	
	//接收内容为json字符串，函数参数加@RequestBody注解（不加就是处理用传统提交方式：application/x-www-form-urlencoded）
	//不清楚为什么没有配置org.springframework.http.converter.json.MappingJacksonHttpMessageConverter也行
	@RequestMapping(value="/json")
	@ResponseBody
	public ResponseMessage testJson(@RequestBody RequestMessage msg) throws Exception{
		//System.out.println(msg.name);
		System.out.println("收到消息：id="+msg.id+"，处理的服务程序："+msg.proc+"，内容："+msg.data);

		//获取spring注入的service对象(负责处理业务的对象)
		String s=this.getClass().getName();
		Field field = this.getClass().getDeclaredField(msg.getProcClassName());
		Object objService = field.get(this); //!!测试时还没有注入，为null
		Class cls = objService.getClass();
		
		//调用消息处理方法并返回对象
		//获取com.cqvie.controller.proc中的消息处理类
//		String packageName= RequestMessage.class.getPackage().getName();
//		Class cls=Class.forName(packageName+".proc."+msg.getProcClassName());
//		Object objProc=cls.newInstance();
		//根据方法名获取方法
		//Method m=cls.getMethod(msg.getMethodName(),new Class[]{});
		Method[] ms=cls.getMethods();
		Method serviceMethod=null;
		String mname=msg.getMethodName();
		for(Method m:ms)
			if(m.getName().equals(msg.getMethodName()))
			{
				serviceMethod=m; break;
			}
		
		if(serviceMethod==null){} //返回错误信息
		//msg.data(json格式)转换为对象
		Class paramType= serviceMethod.getParameterTypes()[0]; //获得第一个参数的类型

		ObjectMapper mapper = new ObjectMapper();
		//Class paramType= Class.forName(packageName+".msg."+msg.type);
		Object objData= mapper.readValue(msg.data, paramType);
		
		ResponseMessage resMsg=new ResponseMessage();
		resMsg.id=msg.id;
		try
		{
			Object retObj=serviceMethod.invoke(objService,objData);
			resMsg.data=mapper.writeValueAsString(retObj);
			resMsg.status=1;
		}
		catch(Exception ex)
		{
			resMsg.status=0;
			resMsg.desc=ex.getMessage();
			System.out.println(ex.getMessage());
		}
		return resMsg;
	}
}
